//
//  HomeHeaderView.m
//  quxiangtao
//
//  Created by E人一铺 on 2018/11/15.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "HomeHeaderView.h"
#import "BtnCollectionView.h"
#import "topView.h"
#import "CycleView.h"

#import "HomeViewController.h"
#import "SecondNavModel.h"

@interface HomeHeaderView()

/**
 顶部四个按钮
 */
@property (nonatomic, strong) topView *topView;


/**
 分类按钮
 */
@property (strong, nonatomic) UIView *btnCollectionContainerView;
@property (strong, nonatomic) BtnCollectionView *collectionView;

/**
 轮播
 */
@property (strong, nonatomic) CycleView *cycleView;

@end



@implementation HomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {

        [self setupUI];
        [self loadData];
    }
    return self;
}

- (void)setParentVC:(UIViewController *)parentVC {
    _parentVC = parentVC;
    self.collectionView.parentVC = parentVC;
    self.cycleView.parentVC = parentVC;
}

- (void)setViewIsSelfSupportwithTag:(NSInteger)tagValue {
    
    if (tagValue != 0) {
        
    }
    
}

- (void)getTbNavgationTitleData:(NSInteger)selectIndex {
    WS(weakSelf);
    [PPNetworkHelper POST:@"user/Welfare/indexbanner" parameters:nil
                  success:^(id responseObject) {
                      NSArray *modArray = [TopNavgatioModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
                   
                      [weakSelf.topView reloadButtonImage:modArray  selectIndex:selectIndex];
                      
                  } failure:^(NSError *error) {
                      
                  }];
}

- (void) loadData {
    
    WS(weakSelf)
//    [self getTbNavgationTitleData];
    
    [self.topView setTopButtonClick:^(NSString *nid) {
        weakSelf.selectIndex = [nid integerValue];
        if ([nid intValue] == 2) {
            weakSelf.collectionView.supportType = 2;
        }
        
        if ([nid intValue] == 3) {
            weakSelf.collectionView.supportType = 3;
        }
        
        if (weakSelf.buttonSelectIndexBlock) {
            weakSelf.buttonSelectIndexBlock([nid integerValue]);
        }
        weakSelf.cycleView.selectIndexBanner = [nid integerValue];
        
        
        NSString *par = [NSString stringWithFormat:@"{\"nid\" : \"%@\"}",[RSA encryptString:nid publicKey:PublicKey]];
        [PPNetworkHelper POST:@"user/Welfare/indexbannertwo" parameters:@{@"json":par} success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 1) {
                NSArray *modelArray = [SecondNavModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
                [weakSelf.collectionView reloadDataWithDataArray:modelArray];
                NSInteger rows = (modelArray.count - 1) / 4 + 1;
                [weakSelf updateLayout: rows * 70];
                HomeViewController *vc = (HomeViewController *)weakSelf.parentVC;
                [vc.header endRefreshing];
            }
        } failure:^(NSError *error) {
            
        }];
    }];
    
}


/**
 请求数据完成，更新约束
 */
- (void) updateLayout:(CGFloat)btnViewHeight {
     __weak typeof(self) weakSelf = self;
    [self.btnCollectionContainerView mas_updateConstraints:^(MASConstraintMaker *make) {

        make.height.mas_equalTo(btnViewHeight);
    }];
    
    [self.cycleView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.mas_top).offset(btnViewHeight+jisuan(15));
    }];
    
    
    
}

- (void) setupUI {
    
    [self addSubview:self.topView];
    [self addSubview:self.btnCollectionContainerView];
    [self.btnCollectionContainerView addSubview:self.collectionView];
    
    [self addSubview:self.cycleView];
    
    __weak typeof(self) weakSelf = self;
    
    CGFloat topHeight = (IS_IPHONE_X || IS_IPHONE_Xr || IS_IPHONE_Xs || IS_IPHONE_Xs_Max) ? jisuan(300) : jisuan(260);
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.mas_top);
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.height.mas_equalTo(topHeight);
    }];
    
    [self.btnCollectionContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.mas_top).offset(jisuan(5));
        make.left.mas_equalTo(weakSelf.mas_left).offset(jisuan(20));
        make.right.mas_equalTo(weakSelf.mas_right).offset(jisuan(-20));
        make.height.mas_equalTo(jisuan(140));
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.btnCollectionContainerView.mas_top).with.offset(0);
        make.left.mas_equalTo(self.btnCollectionContainerView.mas_left).with.offset(0);
        make.right.mas_equalTo(self.btnCollectionContainerView.mas_right).with.offset(0);
        make.bottom.mas_equalTo(self.btnCollectionContainerView.mas_bottom).with.offset(0);
    }];
    
    [self.cycleView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.mas_top).offset(jisuan(112));
        make.left.mas_equalTo(self.mas_left).with.offset(0);
        make.right.mas_equalTo(self.mas_right).with.offset(0);
        make.height.mas_equalTo(jisuanH(300));
    }];
    
}

- (topView *)topView {
    
    if (!_topView) {
        _topView = [[topView alloc] initWithControllerType:0];

    }
    return _topView;
}
- (UIView *)btnCollectionContainerView {
    if (!_btnCollectionContainerView) {
        _btnCollectionContainerView = [[UIView alloc] init];
        _btnCollectionContainerView.backgroundColor = Colorf2f2f2;
        _btnCollectionContainerView.layer.cornerRadius = Scale_W(10);
        _btnCollectionContainerView.layer.masksToBounds = YES;
    }
    return _btnCollectionContainerView;
}

- (CycleView *)cycleView {
    
    if (!_cycleView) {
        _cycleView = [[CycleView alloc] init];
    }
    return _cycleView;
}

- (BtnCollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        
        CGFloat width = (WIDTH - jisuan(40)) / 4.0;
        flowLayout.itemSize = CGSizeMake(width, 70);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        
        _collectionView = [[BtnCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = WhiteColor;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        // 默认为天猫
        _collectionView.supportType = 3;
    }
    return _collectionView;
    
}

@end
